package cn.sdhuijin.healthexam;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import cn.sdhuijin.healthexam.bean.IndexBean;
import cn.sdhuijin.healthexam.bean.Item;
import cn.sdhuijin.healthexam.bean.OrganizationData;
import cn.sdhuijin.healthexam.bean.UserData;
import cn.sdhuijin.healthexam.bean.UserInfoBean;
import cn.sdhuijin.healthexam.util.Constants;
import cn.sdhuijin.healthexam.util.Util;

public class DatabaseHelper extends SQLiteOpenHelper {

	// 录入信息是否录入成功
	public final String COMPLETE_FLAG_OK = "是";
	public final String COMPLETE_FLAG_NO = "否";
	// TAG 标签打印LOG
	private static final String TAG = "DatabaseHelper";
	private Context mContext;

	public DatabaseHelper(Context context) {
		super(context, "DB", null, 1);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.v(TAG, "Test sql");
		db.execSQL("CREATE TABLE IF NOT EXISTS USER(NAME VARCHAR(8) PRIMARY KEY NOT NULL, PASS VARCHAR(8) NOT NULL)");
		executeAssetsSQL(db, "user.csv"); // 生成用户表
		executeAssetsSQL(db, "organization.csv"); // 生成机构表
		executeAssetsSQL(db, "index.csv"); // 生成索引表
		executeAssetsSQL(db, "identity.csv"); // 生成身份证表

		db.execSQL("INSERT INTO 用户表  VALUES('0000','管理员','', '123')");
		db.execSQL("INSERT INTO 机构表  VALUES('0001','山东省济南市高新区')");
		db.execSQL("INSERT INTO 用户表  VALUES('0001','医务人员','0001', '123')");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
	}

	/**
	 * 系统登录
	 * 
	 * @param name
	 *            用户名
	 * @param pass
	 *            密码
	 * @return 成功返回true，失败返回false
	 */
	public boolean login(String name, String pass) {
		Cursor c = this.getReadableDatabase().rawQuery(
				"SELECT * FROM 用户表  WHERE 用户名=? AND 登陆密码=?",
				new String[] { name, pass });
		if (c.getCount() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 查询用户是否存在
	 * 
	 * @param name
	 *            用户名
	 * @return 成功返回true，失败返回false
	 */
	public boolean userIsExits(String name) {
		Cursor c = this.getReadableDatabase().rawQuery(
				"SELECT * FROM 用户表  WHERE 用户名=?", new String[] { name });
		if (c.getCount() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 读取数据库文件（.sql），并执行sql语句
	 * 
	 * @param db
	 * @param 文件名
	 *            例如（user.csv）
	 */
	private void executeAssetsSQL(SQLiteDatabase db, String schemaName) {
		InputStreamReader inputReader = null;
		try {
			Log.v(TAG, "创建数据库");
			inputReader = new InputStreamReader(mContext.getAssets().open(
					"sql" + "/" + schemaName), "UTF-8");
			String create_sql = ReadFile.ReadCSVFile(inputReader);
			Log.v(TAG, "sql ====== " + create_sql);
			db.execSQL(create_sql);
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("db-error", e.toString());
		} finally {
			try {
				if (inputReader != null)
					inputReader.close();
			} catch (IOException e) {
				Log.e("db-error", e.toString());
			}
		}
	}

	/**
	 * 保存数据到机构表
	 * 
	 * @param name
	 *            机构名称
	 */
	public void insertDataToOrganization(String name) {
		SQLiteDatabase sqliteDatabase = null;
		ContentValues values = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			// 数据
			values = new ContentValues();
			values.put("机构编号", Util.getCurrentDateTime());
			values.put("机构名称", name);
			sqliteDatabase.insert("机构表", null, values);
			Log.i(TAG, "保存数据到机构表成功：" + name);
		} catch (Exception e) {
			Log.e(TAG, "保存数据到机构表错误");
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 保存数据到用户表
	 * 
	 * @param name
	 *            用户名姓名
	 */
	public void insertDataToUser(String id, String userName, String password,
			String organization) {
		SQLiteDatabase sqliteDatabase = null;
		ContentValues values = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			// 数据
			values = new ContentValues();
			values.put("用户编号", id);
			values.put("用户名", userName);
			values.put("登陆密码", password);
			values.put("所属机构", organization);
			sqliteDatabase.insert("用户表", null, values);
			Log.i(TAG, "保存数据到用户表成功：" + organization);
		} catch (Exception e) {
			Log.e(TAG, "保存数据到用户表错误");
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param userName
	 */
	public void deleteUser(String userName) {
		SQLiteDatabase sqliteDatabase = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			sqliteDatabase.delete("用户表", "用户名=?", new String[] { userName });
			Log.i(TAG, "用户表数据删除成功：" + userName);
		} catch (Exception e) {
			Log.e(TAG, "用户表删除数据错误");
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 删除机构
	 * 
	 * @param userName
	 */
	public void deleteOrganization(String organizationId) {
		SQLiteDatabase sqliteDatabase = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			sqliteDatabase.delete("机构表", "机构编号=?",
					new String[] { organizationId });
			Log.i(TAG, "机构表数据删除成功：" + organizationId);
			sqliteDatabase.delete("用户表", "所属机构=?",
					new String[] { organizationId });
			Log.i(TAG, "用户表数据删除成功：所诉机构" + organizationId);
		} catch (Exception e) {
			Log.e(TAG, "机构表删除数据错误");
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 机构查询
	 * 
	 * @return 返回 List
	 */
	public List<OrganizationData> searchOrganization() {
		List<OrganizationData> list = null;
		Cursor cur = null;
		try {

			list = new ArrayList<OrganizationData>();
			cur = this.getReadableDatabase().rawQuery(
					"SELECT 机构编号,机构名称  FROM 机构表", null);
			if (cur.getCount() > 0) {
				while (cur.moveToNext()) {
					String id = cur.getString(cur.getColumnIndex("机构编号"));
					String name = cur.getString(cur.getColumnIndex("机构名称"));
					OrganizationData item = new OrganizationData(id, name);
					list.add(item);
				}
			}

		} catch (Exception e) {
			Log.e(TAG, "查询错误");
			e.printStackTrace();
		} finally {
			if (cur != null) {
				cur.close();
			}
		}
		return list;
	}

	/**
	 * 医务人员查询
	 * 
	 * @return 返回 List
	 */
	public List<UserData> searchUser(String organizationId) {
		List<UserData> list = null;
		Cursor cur = null;
		try {

			list = new ArrayList<UserData>();
			cur = this.getReadableDatabase().rawQuery(
					"SELECT 用户编号,用户名  FROM 用户表 WHERE 所属机构 = ?",
					new String[] { organizationId });
			if (cur.getCount() > 0) {
				while (cur.moveToNext()) {
					String id = cur.getString(cur.getColumnIndex("用户编号"));
					String userName = cur.getString(cur.getColumnIndex("用户名"));
					UserData item = new UserData(id, userName);
					list.add(item);
				}
			}

		} catch (Exception e) {
			Log.e(TAG, "查询错误");
			e.printStackTrace();
		} finally {
			if (cur != null) {
				cur.close();
			}
		}
		return list;
	}

	/**
	 * 根据用户名，更新密码
	 * 
	 * @param name
	 * @param pwd
	 */
	public void updateUserPassword(String name, String pwd) {
		SQLiteDatabase sqliteDatabase = null;
		ContentValues values = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			// 数据
			values = new ContentValues();
			values.put("登陆密码", pwd);
			String[] args = { String.valueOf(name) };
			sqliteDatabase.update("用户表", values, "用户名 = ?", args);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "修改密码，保存到数据库错误");
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 根据机构编号，更新机构名称
	 * 
	 * @param name
	 * @param pwd
	 */
	public void updateOrganizationName(String id, String name) {
		SQLiteDatabase sqliteDatabase = null;
		ContentValues values = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			// 数据
			values = new ContentValues();
			values.put("机构名称", name);
			String[] args = { String.valueOf(id) };
			sqliteDatabase.update("机构表", values, "机构编号 = ?", args);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "修改机构名称，保存到数据库错误");
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 通过扫描获取身份证号查询该人员是否存在档案信息
	 * 
	 * @param identifyNum
	 *            身份证号
	 * @param dataType
	 *            表格类型 :档案/体检/随访
	 * @return 检查结果
	 */
	public boolean checkUserExistDoc(String identifyNum, String dataType) {
		boolean exist = false;
		Cursor cursor = null;
		String sql = "SELECT * FROM 索引表  WHERE 身份证号 =? AND 表格类型=?";
		Log.i("SQL", sql);
		try {
			cursor = this.getReadableDatabase().rawQuery(sql,
					new String[] { identifyNum, dataType });
			if (cursor.getCount() > 0) {
				exist = true;
			}
		} catch (Exception e) {
			Log.e(TAG, "检查人员档案错误");
			e.printStackTrace();
		} finally {
			try {
				if (cursor != null) {
					cursor.close();
				}
			} catch (Exception e2) {
			}
		}
		return exist;
	}

	/**
	 * 通过扫描获取身份证号查询身份证表是否存在该人员信息
	 * 
	 * @param identifyNum
	 *            身份证号
	 * @return 检查结果
	 */
	public boolean checkUserExist(String identifyNum) {
		boolean result = false;
		Cursor cursor = null;
		String sql = "SELECT * FROM 身份证表  WHERE 身份证号 =?";
		try {
			cursor = this.getReadableDatabase().rawQuery(sql,
					new String[] { identifyNum });
			if (cursor.getCount() > 0) {
				result = true;
			}
		} catch (Exception e) {
			Log.e(TAG, "检查人员档案错误");
			e.printStackTrace();
		} finally {
			try {
				if (cursor != null) {
					cursor.close();
				}
			} catch (Exception e2) {
			}
		}
		return result;
	}

	/**
	 * 导出查询
	 * 
	 * @param sql
	 *            执行的查询语句
	 * @return 返回 Cursor
	 */
	public Cursor searchExport(String sql) {
		Cursor cursor = null;
		try {
			cursor = this.getReadableDatabase().rawQuery(sql, null);
		} catch (Exception e) {
			Log.e(TAG, "导出查询错误");
			e.printStackTrace();
		}
		return cursor;
	}

	/**
	 * 综合查询
	 * 
	 * @param sql
	 *            执行的查询语句
	 * @return 返回 List
	 */
	public List<Item> searchList(String sql) {
		List<Item> list = null;
		Cursor cur = null;
		try {

			list = new ArrayList<Item>();
			cur = this.getReadableDatabase().rawQuery(sql, null);
			if (cur.getCount() > 0) {
				while (cur.moveToNext()) {
					Item item = new Item();
					item.setName(cur.getString(cur.getColumnIndex("姓名")));
					item.setCardId(cur.getString(cur.getColumnIndex("身份证号")));
					item.setSex(cur.getString(cur.getColumnIndex("性别")));
					item.setBirthDate(cur.getString(cur.getColumnIndex("出生日期")));
					item.setDoctor(cur.getString(cur.getColumnIndex("医生")));
					item.setStyle(cur.getString(cur.getColumnIndex("类型")));
					item.setInputDate(cur.getString(cur.getColumnIndex("录入日期")));
					list.add(item);
				}
			}

		} catch (Exception e) {
			Log.e(TAG, "导出查询错误");
			e.printStackTrace();
		} finally {
			if (cur != null) {
				cur.close();
			}
		}
		return list;
	}

	/**
	 * 保存数据到索引表
	 * 
	 * @param indexBean
	 *            索引表信息
	 */
	public void insertDataToIndex(IndexBean indexBean) {
		SQLiteDatabase sqliteDatabase = null;
		ContentValues values = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			// 数据
			values = new ContentValues();
			values.put("身份证号", indexBean.getIdentifyNum());
			values.put("录入日期", indexBean.getCreateDate());
			values.put("医务人员", indexBean.getDoctorNum());
			values.put("表格类型", indexBean.getDataType());
			values.put("是否完成", indexBean.getCompleteFlag());
			sqliteDatabase.insert("索引表", null, values);
			Log.i(TAG, "保存数据到索引表成功：" + indexBean.getIdentifyNum() + ",类型："
					+ indexBean.getDataType());
		} catch (Exception e) {
			Log.e(TAG, "保存数据到索引表错误");
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 删除索引表中数据
	 * 
	 * @param creatDate
	 */
	public void deleteIndex(String creatDate) {
		SQLiteDatabase sqliteDatabase = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			sqliteDatabase.delete("索引表", "录入日期=?", new String[] { creatDate });
			Log.i(TAG, "索引表数据删除成功：" + creatDate);
		} catch (Exception e) {
			Log.e(TAG, "索引表删除数据错误");
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 保存数据到身份证表
	 * 
	 * @param UserInfoBean
	 *            身份证信息
	 */
	public void insertDataToIdentify(UserInfoBean UserInfoBean) {
		SQLiteDatabase sqliteDatabase = null;
		ContentValues values = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			// 数据
			values = new ContentValues();
			values.put("姓名", UserInfoBean.getUserName());
			values.put("性别", UserInfoBean.getUserSex());
			values.put("民族", UserInfoBean.getUserNation());
			values.put("出生日期", UserInfoBean.getUserBirth());
			values.put("身份证号", UserInfoBean.getIdenfyCardNum());
			values.put("家庭住址", UserInfoBean.getUserAddress());
			values.put("签发机关", UserInfoBean.getFromDepartment());
			values.put("证件有效期", UserInfoBean.getExpiryDate());
			sqliteDatabase.insert("身份证表", null, values);
			Log.i(TAG, "保存数据到身份证表成功：" + UserInfoBean.getIdenfyCardNum());
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "保存数据到身份证表错误");
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 通过身份证号查询身份证信息
	 * 
	 * @param identifyNum
	 *            身份证号
	 * @return 身份证信息
	 */
	public UserInfoBean getIdentifyInfoByIdentifyNum(String identifyNum) {
		SQLiteDatabase sqliteDatabase = null;
		UserInfoBean UserInfoBean = null;
		Cursor cursor = null;
		String sql = "SELECT * FROM 身份证表  WHERE 身份证号 =?";
		try {
			UserInfoBean = new UserInfoBean();
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			cursor = sqliteDatabase.rawQuery(sql, new String[] { identifyNum });
			if (cursor.getCount() > 0) {
				while (cursor.moveToNext()) {
					UserInfoBean.setUserName(cursor.getString(0));
					UserInfoBean.setUserSex(cursor.getString(1));
					UserInfoBean.setUserNation(cursor.getString(2));
					UserInfoBean.setUserBirth(cursor.getString(3));
					UserInfoBean.setIdenfyCardNum(cursor.getString(4));
					UserInfoBean.setUserAddress(cursor.getString(5));
					UserInfoBean.setFromDepartment(cursor.getString(6));
					UserInfoBean.setExpiryDate(cursor.getString(7));
				}
			}
		} catch (Exception e) {
			Log.e(TAG, "查询身份证信息错误");
			e.printStackTrace();
		} finally {
			try {
				// 游标关闭
				if (cursor != null) {
					cursor.close();
				}
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
		return UserInfoBean;
	}

	public void updateIndexTable(String createDate) {
		SQLiteDatabase sqliteDatabase = null;
		ContentValues values = null;
		try {
			// 打开数据库连接
			sqliteDatabase = this.getWritableDatabase();
			// 数据
			values = new ContentValues();
			values.put("是否完成", Constants.COMPLETE_FLAG_OK);
			String[] args = { String.valueOf(createDate) };
			sqliteDatabase.update("索引表", values, "录入日期 = ?", args);
		} catch (Exception e) {
			Log.e(TAG, "更新索引表成功，createDate = " + createDate);
			e.printStackTrace();
		} finally {
			try {
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}

	/**
	 * 检索身份证表，如果不存在当前扫描人员，插入身份证信息到身份证表
	 * 
	 * @param userInfoBean
	 *            身份证信息
	 */
	public void insertInfoIfNotExist(UserInfoBean userInfoBean) {
		SQLiteDatabase sqliteDatabase = null;
		Cursor cursor = null;
		ContentValues values = null;
		String sql = "SELECT * FROM 身份证表  WHERE 身份证号 =?";
		try {
			sqliteDatabase = this.getWritableDatabase();
			cursor = sqliteDatabase.rawQuery(sql,
					new String[] { userInfoBean.getIdenfyCardNum() });
			if (cursor.getCount() <= 0) {
				// 数据
				values = new ContentValues();
				values.put("姓名", userInfoBean.getUserName().trim());
				values.put("性别", userInfoBean.getUserSex());
				values.put("民族", userInfoBean.getUserNation());
				values.put("出生日期", userInfoBean.getUserBirth());
				values.put("身份证号", userInfoBean.getIdenfyCardNum());
				values.put("家庭住址", userInfoBean.getUserAddress());
				values.put("签发机关", userInfoBean.getFromDepartment());
				values.put("证件有效期", userInfoBean.getExpiryDate());
				sqliteDatabase.insert("身份证表", null, values);
				Log.i(TAG, "保存数据到身份证表成功：" + userInfoBean.getIdenfyCardNum());
			}else{
				values = new ContentValues();
				values.put("性别", userInfoBean.getUserSex());
				values.put("民族", userInfoBean.getUserNation());
				values.put("出生日期", userInfoBean.getUserBirth());
				values.put("家庭住址", userInfoBean.getUserAddress());
				values.put("签发机关", userInfoBean.getFromDepartment());
				values.put("证件有效期", userInfoBean.getExpiryDate());
				sqliteDatabase.update("身份证表", values, "身份证号 = ?", new String[] { userInfoBean.getIdenfyCardNum() });
			}
		} catch (Exception e) {
			Log.e(TAG, "检查身份证人员错误,身份证号： " + userInfoBean.getIdenfyCardNum());
			e.printStackTrace();
		} finally {
			try {
				// 关闭游标
				if (!cursor.isClosed()) {
					cursor.close();
				}
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 获取用户档案是否建档完成
	 * 
	 * @param identifyNum
	 * @param dataType
	 * @return
	 */
	public Properties getUserDocCompleteFlag(String identifyNum, String dataType) {
		String flag = "";
		String createDate = "";
		Properties properties = null;
		Cursor cursor = null;
		String sql = "SELECT * FROM 索引表  WHERE 身份证号 =? AND 表格类型=?";
		try {
			properties = new Properties();
			cursor = this.getReadableDatabase().rawQuery(sql,
					new String[] { identifyNum, dataType });
			if (cursor.getCount() > 0) {
				while (cursor.moveToNext()) {
					if (COMPLETE_FLAG_OK.equals(cursor.getString(4))) {
						flag = COMPLETE_FLAG_OK;
						createDate = cursor.getString(1);
					} else if (COMPLETE_FLAG_NO.equals(cursor.getString(4))) {
						flag = COMPLETE_FLAG_NO;
						createDate = cursor.getString(1);
					}
				}
			}
			properties.setProperty("completeFlag", flag);
			properties.setProperty("createDate", createDate);
		} catch (Exception e) {
			Log.e(TAG, "检查人员档案错误");
			e.printStackTrace();
		} finally {
			try {
				if (cursor != null) {
					cursor.close();
				}
			} catch (Exception e2) {
			}
		}
		return properties;
	}

	/**
	 * 照片解析
	 * 
	 * @param schemaName
	 * @return
	 */
	public Bitmap getBitmapFromAssetts(String schemaName) {
		InputStreamReader inputReader = null;
		Bitmap bitmap = null;
		try {
			Log.v(TAG, "创建数据库");
			inputReader = new InputStreamReader(mContext.getAssets().open(
					"photo" + "/" + schemaName));
			bitmap = BitmapFactory.decodeStream(mContext.getAssets().open(
					"photo" + "/" + schemaName));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputReader != null)
					inputReader.close();
			} catch (IOException e) {
				Log.e("db-error", e.toString());
			}
		}
		return bitmap;
	}

	/**
	 * 获取当前档案编辑人员姓名
	 * 
	 * @param createDate
	 *            档案录入日期
	 * @return 人员姓名
	 */
	public String getCurrentEditUserInfo(String createDate) {
		String name = "";
		SQLiteDatabase sqliteDatabase = null;
		Cursor cursor = null;
		String sql = "select 姓名 from 索引表 left join 身份证表 where 身份证表.身份证号 = 索引表.身份证号 and 索引表.录入日期 = ?";
		try {
			sqliteDatabase = this.getReadableDatabase();
			cursor = this.getReadableDatabase().rawQuery(sql,
					new String[] { createDate });
			if (cursor.getCount() > 0) {
				while (cursor.moveToNext()) {
					name = cursor.getString(0);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭游标
				if (cursor != null) {
					cursor.close();
				}
				// 关闭数据库连接
				if (sqliteDatabase != null) {
					sqliteDatabase.close();
				}
			} catch (Exception e2) {
			}
		}
		return name;
	}
}
